﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DataTransferObject;
using DataAccessLayer;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class WizardGCGBPasteNew : Form
    {
        private GCGBPasteMaster gcgbPasteMaster;
        private int gcgbCommandID;

        private CurrencyManager gcgbCommandCurrencyManager;

        private DataTable gcgbCommandDataTable;

        public int GCGBCommandID
        {
            get { return this.gcgbCommandID; }
            set
            {
                if (this.gcgbCommandID != value)
                {
                    this.gcgbCommandID = value;
                }
            }
        }

        public WizardGCGBPasteNew(GCGBPasteMaster gcgbPasteMaster)
        {
            InitializeComponent();

            try
            {
                this.gcgbPasteMaster = gcgbPasteMaster;

                this.GCGBCommandID = this.gcgbPasteMaster.GCGBCommandID;

                gcgbCommandDataTable = this.TableGCGBCommandID();

                gcgbCommandDataTable.PrimaryKey = new DataColumn[] { gcgbCommandDataTable.Columns["GCGBCommandID"] };

                this.GCGBCommandID = this.gcgbPasteMaster.GCGBCommandID;

                this.dataGridViewBG1.AutoGenerateColumns = false;
                this.dataGridViewBG1.DataSource = gcgbCommandDataTable;

                gcgbCommandCurrencyManager = (CurrencyManager)this.BindingContext[gcgbCommandDataTable];

                this.DataBindings.Add("GCGBCommandID", gcgbCommandDataTable, "GCGBCommandID");

                if (gcgbCommandDataTable.Rows.Count > 0)
                {
                    gcgbCommandCurrencyManager.Position = gcgbCommandDataTable.Rows.IndexOf(gcgbCommandDataTable.Rows.Find(this.GCGBCommandID));
                }

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }      

        private DataTable TableGCGBCommandID()
        {
            string SQLString;

            SQLString = "SELECT DISTINCT GCGBCommandDetail.GCGBCommandID, GCGBCommandMaster.GCGBCommandReference, GCGBCommandMaster.EntryDate, GCGBCommandMaster.Description, GCGBCommandMaster.Remarks, GCGBCommandDetail.GCGBCommandDate, GCGBCommandMaster.GCGBCommandReference + ' [' + CONVERT(char(10), GCGBCommandDetail.GCGBCommandDate, 103) + ']' AS GCGBCommandReferenceAndDate ";
            SQLString = SQLString + "FROM GCGBCommandDetail INNER JOIN GCGBCommandMaster ON GCGBCommandDetail.GCGBCommandID = GCGBCommandMaster.GCGBCommandID ";
            SQLString = SQLString + "WHERE GCGBCommandDetail.GCGBCommandID IN (SELECT GCGBCommandID FROM GCGBPasteMaster WHERE GCGBPasteID = " + this.gcgbPasteMaster.GCGBPasteID + ") OR ROUND(GCGBCommandDetail.QuantitySemi - GCGBCommandDetail.QuantityPaste - GCGBCommandDetail.QuantityPasteLoss - GCGBCommandDetail.QuantityPasteBad, 0) > 0  ";

            DataTable tableGCGBCommandID = SQLDatabase.GetDataTable(SQLString);            

            return tableGCGBCommandID;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                this.gcgbPasteMaster.GCGBCommandID = this.GCGBCommandID;
                this.gcgbPasteMaster.GCGBCommandReference = this.dataGridViewBG1.CurrentRow.Cells["GCGBCommandReference"].Value.ToString();
                this.gcgbPasteMaster.GCGBCommandDate = Convert.ToDateTime(this.dataGridViewBG1.CurrentRow.Cells["GCGBCommandDate"].Value);
               
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }




    }
}
